<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>身高预测</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f8f9fa;
            margin: 0;
            padding: 20px;
        }


        h3 {
            color: #343a40;
        }


        .container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 30px;
            max-width: 400px;
            margin: auto;
        }


        input[type="text"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ced4da;
            border-radius: 5px;
        }


        input[type="button"] {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            margin: 5px;
            transition: background-color 0.3s;
            width: 100%;
        }


        input[type="button"]:hover {
            background-color: #0056b3;
        }


        input[type="button"]:active {
            background-color: #003d80;
        }


        #result {
            margin-top: 20px;
            font-weight: bold;
            color: #28a745;
        }


        #error {
            margin-top: 10px;
            font-weight: bold;
            color: red; /* 错误信息颜色为红色 */
        }
    </style>
    <script>
        function kaishijisuan() {
            var father_height = parseFloat(document.getElementById("father_height").value);
            var mother_height = parseFloat(document.getElementById("mother_height").value);
            var gender = document.getElementsByName("gender");
            var result = 0;
            var error = "";


            // 输入验证
            if (isNaN(father_height) || father_height < 140 || father_height > 250) {
    error += "父亲身高输入有误。请重新输入<br>";
}
if (isNaN(mother_height) || mother_height < 120 || mother_height > 250) {
    error += "母亲身高输入有误。请重新输入<br>";
}


            if (error !== "") {
                document.getElementById("error").innerHTML = error;
                document.getElementById("result").innerHTML = ""; // 清空结果
                return; // 退出函数
            } else {
                document.getElementById("error").innerHTML = ""; // 清空错误信息
            }


            for (var i = 0; i < gender.length; i++) {
                if (gender[i].checked) {
                    if (gender[i].value === "male") { // 男
                        result = (father_height + mother_height) / 2 * 1.08;
                    } else if (gender[i].value === "female") {
                        result = (father_height * 0.923 + mother_height) / 2;
                    }
                }
            }
            result = result.toFixed(1);
            document.getElementById("result").innerHTML = "您的儿童身高为：" + result + " 厘米(CM)";
        }


        function chongxinjisuan() {
            document.getElementById("father_height").value = "";
            document.getElementById("mother_height").value = "";
            document.getElementsByName("gender")[0].checked = true;
            document.getElementsByName("gender")[1].checked = false;
            document.getElementById("result").innerHTML = "";
            document.getElementById("error").innerHTML = ""; // 清空错误信息
        }
    </script>
</head>
<body>
    <div class="container">
        <h3>身高预测</h3>
        <hr>
        <h3>儿童身高计算</h3>
        父亲身高：
        <input type="text" id="father_height" placeholder="请输入父亲身高"> 厘米(CM) <br>
        母亲身高：
        <input type="text" id="mother_height" placeholder="请输入母亲身高"> 厘米(CM)<br>
        
        儿童性别：
        <input type="radio" name="gender" value="male" checked>男
        <input type="radio" name="gender" value="female">女<br><br>
        
        <input type="button" value="开始计算" onclick="kaishijisuan()">
        <input type="button" value="重新计算" onclick="chongxinjisuan()"><br>
        
        测试结果：<span id="result"></span>
        <div id="error"></div> <!-- 添加错误信息显示区域 -->
    </div>
    <h6><span>&copy; 2024 @小李不讲理</span></h6>
</body>
</html>